/**
 * @file    dev_afe.c
 * @brief   设备驱动-模拟前端
 * @author  Liuwei
 * @version 1.0.1
 * @date    2023-04-26
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-26 <td>1.0.1     <td>Liuwei   <td>首次创建
 * <tr><td>2024-10-28 <td>1.0.2     <td>Liuwei   <td>新增读取AFE短路保护电流与延时
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "dev_afe.h"
#include "drv_spi.h"
#include "mcu_type.h"
#include "ka495xx_driver.h"
/* macro/define --------------------------------------------------------------*/
#define AFE_ERR_TIMES       (20)
/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/
static uint16_t afe_comm_err_cnt = 0;
static afe_alm_type afe_alarm_msg = {0};
/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
void dev_afe_init(uint8_t cell_num)
{
	vBMIC_InitParams();
	dev_ka495xx_wakeup();
	vBMIC_Ctrl_Startup();
}

uint8_t dev_afe_read_packc_volt(double *curr)
{
	uint8_t ret = 0;

	return ret;	
}

uint8_t dev_afe_read_coulom_volt(double *curr)
{
	uint8_t ret = 0;

	dev_ka495xx_read_curr();

	return ret;	
}

uint8_t dev_afe_read_volt(uint16_t *volt_buf , uint8_t size)
{
	uint8_t ret = 0;
	uint8_t i = 0;

	return ret;	
}

uint8_t dev_afe_read_alarm(afe_alm_type *alarm_msg)
{	
	uint8_t ret = 0;
	uint8_t alarm_state = 0;
	
	alarm_state = usBMICReferRegData(OTHSTAT_ADDR) & OTHSTAT_SCD_OCD_OCC_MASK;
	
	if(alarm_state & ST_SCD_MASK)
		afe_alarm_msg.bit.sc = 1;
	
	*alarm_msg = afe_alarm_msg;

	return ret;	
}

uint8_t dev_afe_ctrl_mos(uint8_t cmos ,uint8_t dmos)
{
	uint8_t ret = 0;

	if(cmos)
	{
		drv_gpio_set_pin(CHG_EN_PORT , CHG_EN_PIN);
	}
	else
	{
		drv_gpio_reset_pin(CHG_EN_PORT , CHG_EN_PIN);
	}
			
    if(dmos)
    {
		drv_gpio_set_pin(DSG_EN_PORT , DSG_EN_PIN);
    }
	else
	{
		drv_gpio_reset_pin(DSG_EN_PORT , DSG_EN_PIN);
	}

	return ret;
}

uint8_t dev_afe_get_mos_state(uint8_t *cmos, uint8_t *dmos)
{
	uint8_t ret = 0;
	
	if(drv_gpio_read_out_pin(CHG_EN_PORT , CHG_EN_PIN) == GPIO_SET)
	{
		*cmos = 1;
	}
	else
	{
		*cmos = 0;
	}
	
	if(drv_gpio_read_out_pin(DSG_EN_PORT , DSG_EN_PIN) == GPIO_SET)
	{
		*dmos = 1;
	}
	else
	{
		*dmos = 0;
	}
	
	return ret;
}

uint8_t dev_afe_clr_alarm(void)
{
	uint8_t ret = 0;
	
	ka495xx_drv_clear_scd();

	return ret;
}

uint8_t dev_afe_read_sc_alarm(void)
{
	return afe_alarm_msg.bit.sc;
}

uint8_t dev_afe_clr_sc_alarm(void)
{
	uint8_t ret = 0;
	
	
	return ret;
}

uint8_t dev_afe_ctrl_balance(uint16_t chal , uint8_t cell_num)
{
	uint8_t ret = 0;	
    
	return ret;
}

uint8_t dev_afe_read_status(void)
{
//	uint8_t ret = FALSE;
//	uint16_t reg_val = 0;
//	ret = oz7717_read_reg(OZ7717_PWRMD, &reg_val);
//	if(TRUE == ret)
//	{
//		if(0 == (reg_val & OZ7717_PWRMD_ACTIVE_MODE))
//		{
//			return FALSE;
//		}
//		
//		return ret;
//	}
//	else
//	{
//	
//		++ afe_comm_err_cnt;

//		return ret;
//	}
}

/**
 * @brief  获取当前AFE通信状态
 * @return uint8_t 
 */
uint8_t dev_afe_read_comm_state(void)
{
	if(afe_comm_err_cnt < AFE_ERR_TIMES)
	{
		return FALSE;
	}
	else
	{
		return TRUE;
	}
}

void dev_afe_into_sleep(void)
{
	dev_ka495xx_into_sleep();
}

uint8_t dev_afe_cadc_ready(void)
{
//	static uint8_t err_cnt = 0;
//	
//	uint16_t state = 0;
//	oz7717_read_reg(OZ7717_STR2, &state);
//	if(state & OZ7717_CONSE_CADC_RDY_FLAG)
//	{
//		err_cnt = 0;
//		oz7717_write_reg(OZ7717_STR2, state);
//		return TRUE;
//	}
//	else
//	{
//		++ err_cnt;
//		if(err_cnt >= 10)
//		{
//			state = 0;
//			oz7717_write_reg(OZ7717_UNLOCK,OZ7717_PATTERN);
//			oz7717_write_reg(OZ7717_CRRTSEL,OZ7717_VREF_ENABLE|OZ7717_CRRTSEL_120uA);//关闭vref;//5A
//			oz7717_write_reg(OZ7717_AUTOSCAN,OZ7717_AUTO_SCAN_SAVE|OZ7717_AUTO_SCAN_EIGHT|OZ7717_AUTO_SCAN_ENABLE);
//			oz7717_write_reg(OZ7717_REG_CADCTRL,CADCTRL_CC_ENABLE|CADCTRL_CONSE_CADC_RDY|0xC0);
//			oz7717_write_reg(OZ7717_UNLOCK,0);
//			err_cnt = 0;
//		}
//		return FALSE;
//	}
}
/*新增20241029*/
uint8_t dev_afe_get_sc_curr_dly(uint16_t *curr , uint16_t *dly)
{
	uint8_t ret = 0;
	uint16_t reg_curr = 0;
	uint16_t reg_delay = 0;
	
	reg_curr = (usBMIC_ReadReg(ALARM_CTRL2_ADDR) >> 0x10) & 0x1F ;
	
	reg_curr = reg_curr * 20000 / (CURR_SMP_RES * 1000);
	
	reg_delay = (usBMIC_ReadReg(ALARM_CTRL3_ADDR) >> 0x10) & 0x1F ;
	
	reg_delay = reg_delay * 31250 / 1000;
	
	*curr = reg_curr;
	
	*dly = reg_delay;
	
	return ret;
}


/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


